package com.qzsoft.zhjd.cement.modules.common.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.zhjd.cement.modules.cementdata.entity.CementDataEntity;
import com.qzsoft.zhjd.cement.modules.cementdata.mapper.CementDataMapper;
import com.qzsoft.zhjd.cement.modules.common.mapper.CementCommonMapper;
import com.qzsoft.zhjd.cement.modules.common.service.CementCommonService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;

/**
 * @ClassName CementCommonServiceImpl
 * @Description 水稳通用业务接口实现
 * @author linhuawei
 * @since 2022-04-24
 */
@Service
public class CementCommonServiceImpl implements CementCommonService {

    @Autowired
    private CementCommonMapper cementCommonMapper;
    
    @Autowired
    private CementDataMapper cementDataMapper;
    
    @Override
    @ZhjdDataFilter
    public CementDataEntity getLastData(Map<String, Object> params) {
        QueryWrapper<CementDataEntity> queryWrapper = new QueryWrapper<CementDataEntity>();
        if(ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("stationCode"))) {
            queryWrapper.eq("station_code", params.get("stationCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchType"))) {
            queryWrapper.eq("match_type", params.get("matchType").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchName"))) {
            queryWrapper.eq("match_name", params.get("matchName").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("matchCode"))) {
            queryWrapper.eq("match_code", params.get("matchCode").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get("day"))) {
            queryWrapper.eq("DATE_FORMAT( discharge_time, '%Y-%m-%d' )", params.get("day").toString());
        }
        if(ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByDesc("discharge_time");
        queryWrapper.last(" limit 1 ");
        return cementDataMapper.selectOne(queryWrapper);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastDay(Map<String, Object> params) {
        Date date = cementCommonMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastMonth(Map<String, Object> params) {
        Date date = cementCommonMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastYear(Map<String, Object> params) {
        Date date = cementCommonMapper.getLastDate(params);
        String result = null;
        if (null != date) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                result = sdf.format(date);
            } catch (Exception e) {
                result = null;
            }
        }
        return result;
    }
    
    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public List<String> getAllYear(Map<String, Object> params) {
        return cementCommonMapper.getAllYear(params);
    }

}
